<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Appoint;
use App\Models\OrderComment;
use App\Models\Service;
use App\Services\NotifyService;
use Dingo\Api\Http\Request;

class OrderCommentController extends BaseController
{

    public $model = OrderComment::class;

    public function index(Request $request) {
        $this->validate($request, [
            'entity_id' => 'required'
        ]);
        $service_id = $request->input('entity_id');
        $list = OrderComment::withCertain('user', ['nickname', 'avatar_url'])
            ->where('entity_id', $service_id)
            ->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));

        $info = null;
        return $this->paginate($list, $info);
    }


    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Throwable
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'comment_id' => 'required',
        ]);
        $id = \request('comment_id');
        $comment = OrderComment::find($id);
        if (!$comment)
            return $this->failed("评论不存在");

        return \DB::transaction(function () use ($comment) {
            $entity_model = new Service();
            $entity_model->where('id', $comment->entity_id)->increment('comment_num', -$comment->reply_count);
            return $this->success($comment->id);
        });
    }

    /**
     * 评论
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Throwable
     */
    public function store(Request $request) {
        $this->validate($request, [
            'appoint_id' => 'required',
            'message' => 'required',
            'star' => 'required',
            'is_anonymous' => 'required'
        ]);
        $user = $this->getUser();
        $input = $request->input();

        try {
            $id = \DB::transaction(function () use($input, $user){
                $appoint = Appoint::find($input['appoint_id']);
                if (!$appoint)
                    throw new \Exception('预约号不存在');

                if ($appoint->user_id != $user->id) {
                    throw new \Exception('无权评价');
                }

                if ($appoint->status != Appoint::STATUS_PAID) {
                    throw new \Exception('当前状态不允许评价');
                }

                OrderComment::create([
                    'user_id' => $user->id,
                    'appoint_id' => $input['appoint_id'],
                    'entity_id' => $appoint->service_id,
                    'message' => $input['message'],
                    'star' => $input['star'],
                    'file' => $input['file'] ?? null,
                    'is_anonymous' => $input['is_anonymous']
                ]);

                if ($appoint->service_id != -1) {
                    $entity_result = Service::find($appoint->service_id);
                    $entity_result->increment('comment_num', 1);
                }

                $appoint->comment_status = Appoint::STATUS_REMARK_ED;
                $appoint->save();

                return $input['appoint_id'];
            });
            return $this->success($id);
        } catch (\Exception $e) {
            \Log::error($e->getLine() . $e->getMessage());
            return $this->failed($e->getMessage());
        }

    }

}

